package org.larkdoc.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.larkdoc.dao.GroupInfoMapper;
import org.larkdoc.model.bo.GroupBO;
import org.larkdoc.model.entity.GroupInfo;
import org.larkdoc.model.entity.GroupRelUser;
import org.larkdoc.service.GroupRelUserService;
import org.larkdoc.service.GroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 组业务功能实现类 
 * 
 * @author  zhangpeijun[zhangpeijun1024@qq.com]
 * @version  [v1.0.1, 2017年10月17日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Service("groupService")
public class GroupServiceImpl implements GroupService {
    
    private final static Logger log = LoggerFactory.getLogger(GroupServiceImpl.class);
    
    @Autowired
    GroupInfoMapper             mapper;
    
    @Autowired
    GroupRelUserService         groupRelUserService;
    
    @Override
    public int deleteByPrimaryKey(Integer id) {
        if (id > 0) {
            return mapper.deleteByPrimaryKey(id);
        } else {
            return 0;
        }
    }
    
    @Override
    public int insert(GroupInfo record) {
        if (null != record) {
            return mapper.insert(record);
        } else {
            return 0;
        }
    }
    
    @Override
    public int insertSelective(GroupInfo record) {
        if (null != record) {
            return mapper.insertSelective(record);
        } else {
            return 0;
        }
    }
    
    @Override
    public GroupInfo selectByPrimaryKey(Integer id) {
        if (id > 0) {
            return mapper.selectByPrimaryKey(id);
        } else {
            return null;
        }
    }
    
    @Override
    public int updateByPrimaryKeySelective(GroupInfo record) {
        if (null != record && record.getId() > 0) {
            return mapper.updateByPrimaryKeySelective(record);
        } else {
            return 0;
        }
    }
    
    @Override
    public int updateByPrimaryKey(GroupInfo record) {
        if (null != record && record.getId() > 0) {
            return mapper.updateByPrimaryKey(record);
        } else {
            return 0;
        }
    }
    
    @Override
    public int insertSelective(GroupBO record) {
        log.debug("insertSelective param {}", record);
        int result = 0;
        if (null != record) {
            GroupInfo groupInfo = record.getGroupInfo();
            result = insertSelective(groupInfo);
            if (result == 1) {
                int id = groupInfo.getId();
                List<GroupRelUser> users = record.getGroupRelUsers();
                if (CollectionUtils.isNotEmpty(users)) {
                    for (GroupRelUser user : users) {
                        user.setGroupId(id);
                    }
                }
                groupRelUserService.insertBatch(users);
            } else {
                log.error("insertSelective result is zero. param : {}", record);
            }
        }
        return result;
    }
    
}
